import Vue from 'vue'
import Router from 'vue-router'
import { getToken, getMenu } from '@/utils/auth'

Vue.use(Router)

/* Layout */
import Layout from '@/layout'
import labelRouter from './modules/label'
/**
 * Note: sub-menu only appear when route children.length >= 1
 * Detail see: https://panjiachen.github.io/vue-element-admin-site/guide/essentials/router-and-nav.html
 *
 * hidden: true                   if set true, item will not show in the sidebar(default is false)
 * alwaysShow: true               if set true, will always show the root menu
 *                                if not set alwaysShow, when item has more than one children route,
 *                                it will becomes nested mode, otherwise not show the root menu
 * redirect: noRedirect           if set noRedirect will no redirect in the breadcrumb
 * name:'router-name'             the name is used by <keep-alive> (must set!!!)
 * meta : {
    roles: ['admin','editor']    control the page roles (you can set multiple roles)
    title: 'title'               the name show in sidebar and breadcrumb (recommend set)
    icon: 'svg-name'             the icon show in the sidebar
    breadcrumb: false            if set false, the item will hidden in breadcrumb(default is true)
    activeMenu: '/example/list'  if set path, the sidebar will highlight the path you set
  }
 */

/**
 * constantRoutes
 * a base page that does not have permission requirements
 * all roles can be accessed
 */

const nodesField = 'children'
const getSideMenu = (menuArr, rootMenu) => {
  const menus = []
  for (let index = 0; index < rootMenu.length; index++) {
    const element = rootMenu[index]
    // eslint-disable-next-line no-constant-condition
    if ((element.meta && ~menuArr.indexOf(element.meta.title)) || !element.meta) {
      menus.push(element)
      var childrenNodes = element[nodesField]
      if (childrenNodes && childrenNodes.length > 0) {
        const childrenMenu = getSideMenu(menuArr, childrenNodes)
        element[nodesField] = childrenMenu
      }
    }
  }
  return menus
}

const filterRouter = (routes, menu) => {
  menu = menu || []
  let menus = menu.map(item => item.name)
  menus = menus.concat('首页', '系统首页')
  return getSideMenu(menus, routes)
}

const allRouters = [
  // 首页
  {
    // 路由
    path: '/',
    component: Layout,
    // 重定向地址，在面包屑中点击会重定向去的地址
    redirect: '/home',
    // 路由
    name: '首页',
    meta: {
      title: '首页',
      icon: 'example'
    },
    children: [{
      path: 'home',
      name: 'home',
      // 跳转组件的方向
      component: () => import('@/views/home/home/index'),
      meta: {
        title: '系统首页',
        icon: 'dashboard'
      }
    }, {
      path: 'accountSetting',
      name: 'accountSetting',
      component: () => import('@/views/home/accountSetting/index'),
      meta: {
        title: '账户设置',
        icon: 'table'
      }
    },
    {
      path: 'operationLog',
      name: 'operationLog',
      component: () => import('@/views/home/operationLog/index'),
      meta: {
        title: '操纵日志',
        icon: 'tree'
      }
    }
    ]
  },
  // 用户管理
  labelRouter,
  // {
  //   path: '/user',
  //   component: Layout,
  //   redirect: '/user',
  //   name: '',
  //   meta: { title: '用户管理', icon: 'example' },
  //   children: [
  //     {
  //       path: 'user-manage',
  //       name: 'User-manage',
  //       component: () => import('@/views/userManage/user-manage/index'),
  //       meta: { title: '用户管理'}
  //     },
  //     {
  //       path: 'user-industry',
  //       name: 'User-industry',
  //       component: () => import('@/views/userManage/user-industry/index'),
  //       meta: { title: '行业分类'}
  //     },
  //     {
  //       path: 'label-manage',
  //       name: 'Label-manage',
  //       component: () => import('@/views/userManage/label-manage/index'),
  //       meta: { title: '标签管理'},
  //     },
  //     {
  //       path:'hotlabel-manage',
  //       name:'Hotlabel-manage',
  //       component: () => import('@/views/userManage/hotlabel-manage/index'),
  //       meta:{title:'用户活跃标签管理'}
  //     },
  //     {
  //       path:'sendlabel-manage',
  //       name:'Sendlabel-manage',
  //       component: () => import('@/views/userManage/sendlabel-manage/index'),
  //       meta:{title:'信息发布标签设置'}
  //     },

  //     {
  //       path: 'level-manage',
  //       name: 'Level-manage',
  //       component: () => import('@/views/userManage/level-manage/index'),
  //       meta: { title: '用户等级管理' }
  //     },
  //     {
  //       path: 'coin-query',
  //       name: 'Coin-query',
  //       component: () => import('@/views/userManage/coin-query/index'),
  //       meta: { title: '金币与聚合金币值查询'}
  //     },
  //    {
  //       path: 'classify',
  //       name: 'classify',
  //       component: () => import('@/views/userManage/classify/index'),
  //     },
  //     {
  //       path: 'user-detail',
  //       name: 'User-detail',
  //       component: () => import('@/views/userManage/user-detail/index'),
  //     },
  //   ]
  // },

  // 运营
  {
    path: '/operate',
    component: Layout,
    // redirect: '/example/run',
    name: 'operate',
    meta: {
      title: '运营',
      icon: 'example'
    },
    children: [
      // {
      //   path: 'operate_1',
      //   name: 'operate_1',
      //   component: () => import('@/views/operate/operate_1/index'),
      //   meta: {
      //     title: '购买力筛选'
      //   }
      // },
      {
        path: 'operate_2',
        name: 'operate_2',
        component: () => import('@/views/operate/operate_2/index'),
        meta: {
          title: '发布信息管理'
        }
      },
      // {
      //   path: 'operate_3',
      //   name: 'operate_3',
      //   component: () => import('@/views/operate/operate_3/index'),
      //   meta: {
      //     title: '推送方式设置管理'
      //   }
      // },
      {
        path: 'operate_4',
        name: 'operate_4',
        component: () => import('@/views/operate/operate_4/index'),
        meta: {
          title: '文档管理'
        }
      },
      {
        path: 'operate_4/addTo',
        name: 'addTo',
        hidden: true,
        component: () => import('@/views/operate/operate_4/addTo/index')
      },
      {
        path: 'operate_4/seeContent',
        name: 'seeContent',
        hidden: true,
        component: () => import('@/views/operate/operate_4/seeContent/index')
      },
      {
        path: 'operate_4/edit',
        name: 'edit',
        hidden: true,
        component: () => import('@/views/operate/operate_4/edit/index')
      },
      {
        path: 'operate_4/release',
        name: 'release',
        hidden: true,
        component: () => import('@/views/operate/operate_4/release/index')
      },
      {
        path: 'operate_5',
        name: 'operate_5',
        component: () => import('@/views/operate/operate_5/index'),
        meta: {
          title: '发布信息模板设置'
        }
      },
      {
        path: 'operate_6',
        name: 'operate_6',
        component: () => import('@/views/operate/operate_6/index'),
        meta: {
          title: 'APP首页轮播图管理'
        }
      },
      {
        path: 'operate_7',
        name: 'operate_7',
        component: () => import('@/views/operate/operate_7/index'),
        meta: {
          title: '金币购买与人民币关联'
        }
      }
    ]
  },

  // 订单
  {
    path: '/order',
    component: Layout,
    name: 'order',
    meta: {
      title: '订单',
      icon: 'example'
    },
    children: [{
      path: 'orderList',
      name: 'orderList',
      component: () => import('@/views/order/orderList/orderList'),
      meta: {
        title: '订单列表',
        icon: 'form'
      }
    },
    {
      path: 'orderSetting',
      name: 'orderSetting',
      component: () => import('@/views/order/orderSetting/orderSetting'),
      meta: {
        title: '订单设置',
        icon: 'form'
      }
    },
    {
      path: 'orderDetails',
      name: 'orderDetails',
      component: () => import('@/views/order/orderDetails/orderDetails')
    }
    ]
  },

  // 统计
  {
    path: '/statistics',
    component: Layout,
    name: 'Statistics',
    meta: {
      title: '统计',
      icon: 'example'
    },
    children: [{
      path: 'statistics_tran',
      name: 'statistics_tran',
      component: () => import('@/views/statistics/statistics_tran/index'),
      meta: {
        title: '交易统计',
        icon: 'form'
      }
    },
    {
      path: 'statistics_stat',
      name: 'statistics_stat',
      component: () => import('@/views/statistics/statistics_stat/index'),
      meta: {
        title: '流量统计',
        icon: 'form'
      }
    },
    {
      path: 'statistics_user',
      name: 'statistics_user',
      component: () => import('@/views/statistics/statistics_user/index'),
      meta: {
        title: '用户统计',
        icon: 'form'
      }
    },
    {
      path: 'statistics_search',
      name: 'statistics_search',
      component: () => import('@/views/statistics/statistics_search/index'),
      meta: {
        title: '搜索统计',
        icon: 'form'
      }
    }
    ]
  },
  // 资金

  {
    path: '/capital',
    component: Layout,
    // redirect: '/nested/menu1',
    name: 'capital',
    meta: {
      title: '资金',
      icon: 'example'
    },
    children: [{
      path: 'comprehensive',
      component: () => import('@/views/capital/comprehensive/index'), // Parent router-view
      name: 'comprehensive',
      meta: {
        title: '综合统计',
        icon: 'form'
      }
    },
    {
      path: 'user',
      component: () => import('@/views/capital/user/index'),
      meta: {
        title: '用户排行',
        icon: 'form'
      }
    },
    {
      path: 'Sale',
      component: () => import('@/views/capital/Sale/index'),
      meta: {
        title: '销售统计',
        icon: 'form'
      }
    },
    {
      path: 'reconciliationList',
      name: 'reconciliationList',
      component: () => import('@/views/capital/reconciliationList/index'),
      meta: {
        title: '对账列表',
        icon: 'form'
      }
    },
    {
      path: 'reconDetail',
      hidden: true,
      component: () => import('@/views/capital/reconciliationList/reconDetail/index'), // Parent router-view
      name: 'reconDetail',
      meta: {
        title: '对账详情'
      }
    }
    ]
  },
  // 权限
  {
    path: '/divisionManagement',
    component: Layout,
    // redirect: '/example/table',
    name: 'divisionManagement',
    meta: {
      title: '权限',
      icon: 'example'
    },
    children: [{
      path: 'divisionManagement',
      name: 'divisionManagement',
      component: () => import('@/views/jurisdiction/divisionManagement/index'),
      meta: {
        title: '部门管理',
        icon: 'table'
      }
    },
    {
      path: 'operationLog',
      name: 'operationLog',
      component: () => import('@/views/jurisdiction/operationLog/index'),
      meta: {
        title: '操纵日志',
        icon: 'tree'
      }
    },
    {
      path: 'addEmployee',
      name: 'addEmployee',
      component: () => import('@/views/jurisdiction/addEmployee/index')
    },
    // {
    //   path: 'databaseManagement ',
    //   name: 'databaseManagement',
    //   component: () => import('@/views/jurisdiction/databaseManagement/index'),
    //   meta: {
    //     title: '数据库管理',
    //     icon: 'tree'
    //   }
    // },
    {
      path: 'staffManagement',
      name: 'staffManagement',
      component: () => import('@/views/jurisdiction/staffManagement/index'),
      meta: {
        title: '员工管理',
        icon: 'tree'
      }
    },
    {
      path: 'department',
      name: 'department',
      component: () => import('@/views/jurisdiction/department/index')
    },
    {
      path: 'editingStaff',
      name: 'editingStaff',
      component: () => import('@/views/jurisdiction/editingStaff/index')
    },
    {
      path: 'progPermission',
      name: 'progPermission',
      component: () => import('@/views/jurisdiction/progPermission/index')
    }
    ]
  },

  // 设置
  {
    path: '/setting',
    component: Layout,
    // redirect: '/example/table',
    name: 'Example',
    meta: {
      title: '设置',
      icon: 'example'
    },
    children: [{
      path: 'userType',
      name: 'userType',
      component: () => import('@/views/setting/userType/index'),
      meta: {
        title: '用户类型设置',
        icon: 'table'
      }
    },
    {
      path: 'informationType',
      name: 'informationType',
      component: () => import('@/views/setting/informationType/index'),
      meta: {
        title: '信息类型',
        icon: 'tree'
      }
    },
    {
      path: 'zoneSetting',
      name: 'zoneSetting',
      component: () => import('@/views/setting/zoneSetting/index'),
      meta: {
        title: '区域设置',
        icon: 'tree'
      }
    },
    {
      path: 'InformationPlatform',
      name: 'InformationPlatform',
      component: () => import('@/views/setting/InformationPlatform/index'),
      meta: {
        title: '平台信息',
        icon: 'tree'
      }
    },
    {
      path: 'newsWarn',
      name: 'newsWarn',
      component: () => import('@/views/setting/newsWarn/index'),
      meta: {
        title: '消息提醒',
        icon: 'tree'
      }
    },
    {
      path: 'payMents',
      name: 'payMents',
      component: () => import('@/views/setting/payMents/index'),
      meta: {
        title: '支付设置',
        icon: 'tree'
      }
    },
    // 编辑支付方式
    {
      path: 'payForEditing',
      name: 'payForEditing',
      component: () => import('@/views/setting/payForEditing/index')
    }

      // {
      //   path: 'verificationCodeSetting',
      //   name: 'verificationCodeSetting',
      //   component: () => import('@/views/setting/verificationCodeSetting/index'),
      //   meta: { title: '验证码设置', icon: 'tree' }
      // }
    ]
  }
]

export const constantRoutes = [{
  path: '/login',
  component: () => import('@/views/login/index'),
  hidden: true
},
{
  path: '/404',
  component: () => import('@/views/404'),
  hidden: true
},
...filterRouter(allRouters, getMenu()),
// 404 page must be placed at the end !!!
{
  path: '*',
  redirect: '/404',
  hidden: true
}
]

const createRouter = () => new Router({
  // mode: 'history', // require service support
  scrollBehavior: () => ({
    y: 0
  }),
  routes: constantRoutes
})

const router = createRouter()

router.beforeEach((to, from, next) => {
  const isLogin = getToken()
  if (isLogin) {
    if (to.path == '/login') {
      // 如果用户信息存在则往下执行。
      next('/')
    } else {
      next()
    }
  } else {
    // 如果用户token不存在则跳转到login页面
    if (to.path == '/login') {
      next()
    } else {
      next('/login')
    }
  }
})

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export default router
